package com.project.common.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.project.service.sys.CacheService;
import com.project.entity.sys.Area;


public class AreaTag extends TagSupport {

	private static final long serialVersionUID = -7762361372178555026L;

	private String proValue = "";
	private String cityValue = "";
	private String munValue = "";
	
	public int doStartTag() throws JspException {
		try {
			this.pageContext.getOut().write(getAreaName());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	private String getAreaName() {
		try {
            WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
            CacheService cacheService = (CacheService)wac.getBean("cacheService");
            StringBuffer html = new StringBuffer();
            Map<String, Area> proCityMun = cacheService.getProCityMun();
            if(StringUtils.isNotEmpty(proValue)&&proCityMun.get(proValue)!=null){
            	html.append(proCityMun.get(proValue).getName());
            	if(StringUtils.isNotEmpty(cityValue)){
            		HashMap<String, Area> map=proCityMun.get(proValue).getAityMap();
            		if(map!=null&&map.get(cityValue)!=null){
            			html.append(map.get(cityValue).getName());
            			if(StringUtils.isNotEmpty(munValue)){
            				html.append(map.get(cityValue).getAityMap().get(munValue).getName());
            			}
            		}
            	}
            }
            return html.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
	}

	public String getProValue() {
		return proValue;
	}

	public void setProValue(String proValue) {
		this.proValue = proValue;
	}

	public String getCityValue() {
		return cityValue;
	}

	public void setCityValue(String cityValue) {
		this.cityValue = cityValue;
	}

	public String getMunValue() {
		return munValue;
	}

	public void setMunValue(String munValue) {
		this.munValue = munValue;
	}

}
